package com.ehualu.core.util.sms.service;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.ehualu.core.property.PropertyConfig;
import com.ehualu.core.util.sms.pojo.Sms;
import com.ehualu.core.util.sms.pojo.SmsResponse;
import com.ehualu.core.util.sms.pojo.SmsResponseStatus;

/**
 * 华录亿动短信接口
 * @author Administrator
 *
 */
public class HualuEdSmsService{

private static final Logger logger = LoggerFactory.getLogger(HualuEdSmsService.class);
	
	/**
	 * 短信发送(华录亿动)
	 * @param phone
	 * @param msg
	 * @return
	 */
	/**
	 * 短信发送
	 * 
	 * @param phone
	 * @param msg
	 * @return
	 */
	public String sendSms(Sms sms) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			// 您的验证码是236598，请于3分钟内正确输入，收到信息的请在QQ上回一声【易华录】
			StringBuilder sb = new StringBuilder();
			sb.append(PropertyConfig.getProperty("SMS_URL")).append("?cpid=")
					.append(PropertyConfig.getProperty("SMS_ID"))
					.append("&cppwd=")
					.append(PropertyConfig.getProperty("SMS_PWD"))
					.append("&phone=").append(sms.getPhone()).append("&msgcont=")
					.append(URLEncoder.encode(PropertyConfig.getProperty("SMS_SIGN")+sms.getMessage(), "gb2312"));
			HttpGet httpget = new HttpGet(sb.toString());
			logger.info("执行短信发送: " + httpget.getURI());
			// 执行get请求.
			CloseableHttpResponse response = httpclient.execute(httpget);
			// 获取响应实体
			HttpEntity entity = response.getEntity();
			logger.info("--------------------------------------");
			// 打印响应状态
			logger.info(response.getStatusLine().toString());
			String entityString = null;
			if (entity != null) {
				// 打印响应内容长度
				logger.info("响应内容长度: " + entity.getContentLength());
				entityString = EntityUtils.toString(entity);
				// 打印响应内容
				logger.info("响应内容: " + entityString);
				logger.info("------------------------------------");
				if(("0").equals(entityString)){
					return JSON.toJSONString(new SmsResponse(SmsResponseStatus.SUCCESS.getStatus(),SmsResponseStatus.SUCCESS.getCode(),SmsResponseStatus.SUCCESS.getReason()));
				}else if(("ERROR").equals(entityString.split("&")[0])){
					return JSON.toJSONString(new SmsResponse(SmsResponseStatus.FAIL.getStatus(),SmsResponseStatus.FAIL.getCode(),entityString.split("&")[1]));
				}else{
					return JSON.toJSONString(new SmsResponse(SmsResponseStatus.RESULT_IS_ERROR.getStatus(),SmsResponseStatus.RESULT_IS_ERROR.getCode(),SmsResponseStatus.RESULT_IS_ERROR.getReason()));
				}
			}else{
				return JSON.toJSONString(new SmsResponse(SmsResponseStatus.RESULT_IS_NULL.getStatus(),SmsResponseStatus.RESULT_IS_NULL.getCode(),SmsResponseStatus.RESULT_IS_NULL.getReason()));
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return JSON.toJSONString(new SmsResponse(SmsResponseStatus.REQUEST_ERROR.getStatus(),SmsResponseStatus.REQUEST_ERROR.getCode(),SmsResponseStatus.REQUEST_ERROR.getReason()));
		} catch (ParseException e) {
			e.printStackTrace();
			return JSON.toJSONString(new SmsResponse(SmsResponseStatus.DATA_PARSE_EXCEPTION.getStatus(),SmsResponseStatus.DATA_PARSE_EXCEPTION.getCode(),SmsResponseStatus.DATA_PARSE_EXCEPTION.getReason()));
		} catch (IOException e) {
			e.printStackTrace();
			return JSON.toJSONString(new SmsResponse(SmsResponseStatus.SYSTEM_IS_EXCEPTION.getStatus(),SmsResponseStatus.SYSTEM_IS_EXCEPTION.getCode(),SmsResponseStatus.SYSTEM_IS_EXCEPTION.getReason()));
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
				return JSON.toJSONString(new SmsResponse(SmsResponseStatus.CLOSE_HTTPCLIENT_FAIL.getStatus(),SmsResponseStatus.CLOSE_HTTPCLIENT_FAIL.getCode(),SmsResponseStatus.CLOSE_HTTPCLIENT_FAIL.getReason()));
			}
		}
	}
}
